package com.ynny.jdbc01;

import com.mysql.cj.jdbc.Driver;

import java.sql.*;

public class JDBCDemo02 {


    public static void main(String[] args) throws SQLException {
        //1.创建一个驱动  光盘
//        new Driver()
        Driver driver = new Driver();
        //2.安装 注册 驱动
        DriverManager.registerDriver(driver);
        //3.获取连接
        /**
         * localhost --->本地IP  127.0.0.1
         * String url  ----->  协议 和 厂商  IP 端口
         * String user -----  用户名
         * String password    密码
         */
        String url = "jdbc:mysql://localhost:3306/db01";
        String userName = "root";
        String password = "root";
        Connection connection = DriverManager
                .getConnection(url, userName, password);

        //写sql
        String sql = "select * from book";
        //4.准备一个PreparedStatement对象 装载运输SQL语句到数据库中
        PreparedStatement preparedStatement =
                connection.prepareStatement(sql);
        //5.执行sql语句
        ResultSet resultSet = preparedStatement.executeQuery();
        //6.解析 ResultSet
        while (resultSet.next()){
            String id = resultSet.getString("id");
            String name = resultSet.getString("name");
            System.out.println(id + "\t" +name);
        }

        //7.关闭资源
        resultSet.close();
        preparedStatement.close();
        connection.close();



    }
}
